package com.liyunc.demo.resources;
/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import com.liyunc.demo.model.dto.GenericDto;
import com.liyunc.demo.model.dto.RoleInfoDto;
import com.liyunc.demo.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-02-18
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-02-18                       Created
 */

@RestController
@RequestMapping(value = "/api/v1/role", produces = {MediaType.APPLICATION_JSON_VALUE})
public class RoleResource {

    @Autowired
    private RoleService roleService;

    @GetMapping(value = "/{userId}")
    @Operation(description = "获取单个用户的所有项目访问权限")
    public GenericDto<List<RoleInfoDto>> getRoleListByUserId(
        @Valid @Min(0) @RequestParam(value = "startIndex", defaultValue = "0") int startIndex,
        @Valid @Min(1) @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
        @Min(1) @PathVariable("userId") long userId) {
        return GenericDto
            .of(roleService.getProjectListWithRoleByUserId(startIndex, pageSize, userId));
    }

//    @PostMapping(value = "add-role", consumes = {MediaType.APPLICATION_JSON_VALUE})
//    @Operation(description = "给用户授权")
//    public GenericDto<RoleInfoDto> addRoleWithProject(@RequestBody RoleInfoDto infoDto) {
//        return GenericDto
//            .of(roleService.setRoleWithProject(
//                infoDto.getKey()).orElse(null));
//    }

    @GetMapping(value = "delete-role")
    @Operation(description = "删除用户权限")
    public GenericDto<RoleInfoDto> deleteRole(
        @RequestParam(value = "userId") long userId,
        @RequestParam(value = "projectId") long projectId
    ) {
        return GenericDto
            .of(roleService.deleteRoleWithProject(userId, projectId).orElse(null));
    }


}
